package cn.walls1717.leetcode.array;

import java.util.ArrayList;
import java.util.List;

/**
 * 54. 螺旋矩阵
 * 力扣地址：https://leetcode.cn/problems/spiral-matrix/
 *
 * @author walls1717
 * @since 2022/5/10
 **/
public class T54 {

    public static void main(String[] args) {

        int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
        int[][] matrix2 = {{1, 2, 3, 4}};
        int[][] matrix3 = {{7}, {9}, {6}};
        int[][] matrix4 = {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}, {11, 12, 13}, {14, 15, 16}};
        int[][] matrix5 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(matrix2.length);
        List<Integer> integers = spiralOrder2(matrix5);
        System.out.println(integers);

    }

    public static List<Integer> spiralOrder2(int[][] matrix) {
        List<Integer> list = new ArrayList<Integer>();
        // 如果数组为空，数组的行为 0 ，则直接返回空集合
        if (matrix == null || matrix.length == 0) {
            return list;
        }

        // 二维数组行
        int m = matrix.length;
        // 二维数组列
        int n = matrix[0].length;
        // 控制循环
        int i = 0;

        // 统计矩阵从外向内的层数，如果矩阵非空，那么它的层数至少为1层
        int count = (Math.min(m, n) + 1) / 2;
        // 从外部向内部遍历，逐层打印数据
        while (i < count) {
            for (int j = i; j < n - i; j++) {
                list.add(matrix[i][j]);
            }
            for (int j = i + 1; j < m - i; j++) {
                list.add(matrix[j][(n - 1) - i]);
            }

            for (int j = (n - 1) - (i + 1); j >= i && (m - 1 - i != i); j--) {
                list.add(matrix[(m - 1) - i][j]);
            }
            for (int j = (m - 1) - (i + 1); j >= i + 1 && (n - 1 - i) != i; j--) {
                list.add(matrix[j][i]);
            }
            i++;
        }
        return list;
    }

    /**
     * 我自己未实现的方法
     * 但是整体思路是对的
     *
     * @param matrix 二维数组
     * @return 。
     */
    public static List<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        if (matrix.length == 1) {
            for (int i = 0; i < matrix[0].length; i++) {
                arrayList.add(matrix[0][i]);
            }
            return arrayList;
        }
        if (matrix[0].length == 1) {
            for (int[] ints : matrix) {
                arrayList.add(ints[0]);
            }
            return arrayList;
        }
        int startX = 0;
        int startY = 0;
        int offset = 1;
        int count = 0;
        int loop = Math.max(matrix.length / 2, matrix[0].length / 2);
        while (loop > 0) {
            int i = startX;
            int j = startY;
            for (; j < startX + matrix[0].length - offset; ++j) {
                arrayList.add(matrix[startX][j]);
            }
            for (; i < startY + matrix.length - offset; ++i) {
                arrayList.add(matrix[i][j]);
            }
            for (; j > startY; j--) {
                arrayList.add(matrix[i][j]);
            }
            for (; i > startX; i--) {
                if (count == 0 || matrix.length / 2 != i) {
                    arrayList.add(matrix[i][j]);
                }
            }


            loop--;
            count++;
            startX += 1;
            startY += 1;
            offset += 2;
        }
        if (matrix.length % 2 == 1 && matrix[0].length % 2 == 1 && matrix.length == matrix[0].length) {
            arrayList.add(matrix[matrix.length / 2][matrix[0].length / 2]);
        }
        return arrayList;
    }

}
